package cn.buildoncloud.labor.workermanapplet.business.modules.services.impl;

import cn.buildoncloud.labor.workermanapplet.business.modules.services.WorkerManAttendanceService;
import cn.buildoncloud.labor.workermanapplet.common.base.service.BaseServiceImpl;
import cn.buildoncloud.labor.workermanapplet.common.page.Pagination;
import cn.buildoncloud.labor.workermanapplet.util.StringUtil;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Record;
import org.nutz.dao.sql.Sql;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class WorkerManAttendanceServiceImpl extends BaseServiceImpl<Map> implements WorkerManAttendanceService {


    public WorkerManAttendanceServiceImpl(Dao dao) {
        super(dao);
    }


    @Override
    public Pagination listPage(Long pId, String search, Integer pageNumber, int pageSize) {
//        boolean b = this.tableIsExist("bi_worker_ad_mounth_", StringUtil.last2num(pId.toString()));
//        if (!b) {
//            return new Pagination();
//        }
        String projectId = pId.toString();
        String afterProjectId = projectId.substring(projectId.length()-2, projectId.length());
        StringBuilder sb = new StringBuilder(" SELECT CONCAT(w_m.id, '') worker_man_id, CONCAT(w_m.head_photo_id,'') head_photo_id, CONCAT(w_p.project_id, '') project_id, w_m.`name` worker_man_name, w_t.`name` worker_team_name, w_m.sex, w_m.birth_date, TIMESTAMPDIFF(YEAR, w_m.birth_date, CURDATE()) age ,   w_m.tel, CONCAT(w_class.`name`,'-',w_type.`name`) worker_type, w_a_m.total_working_time, w_a_m.attendance_days ");
        sb.append(" FROM bi_workerman w_m ");
        sb.append(" LEFT JOIN bi_re_workman_and_teamproject w_tp on w_tp.worker_man_id = w_m.id and wages_pattern = 1 ");
        sb.append(" LEFT JOIN bi_workerteam w_t ON w_m.worker_team_id = w_t.id ");
        sb.append(" LEFT JOIN bi_re_workerteam_project w_p ON w_p.worker_team_id = w_t.id and w_p.wp_tates = 0 ");
        sb.append(" LEFT JOIN bi_worker_man_work_extend w_m_w_e ON w_m_w_e.worker_man_id = w_m.id ");
        sb.append(" LEFT JOIN bi_worker_ad_mounth_");
        sb.append(afterProjectId);
        sb.append(" w_a_m ON w_a_m.worker_id = w_m.id and w_a_m.project_id = @pId");
        sb.append(" LEFT JOIN sys_work_class w_class ON w_m_w_e.work_class_id = w_class.id ");
        sb.append(" LEFT JOIN sys_work_type w_type ON w_m_w_e.work_type_id = w_type.id ");
        sb.append(" WHERE  w_p.project_id = @pId  and w_tp.project_id = @pId ");

        if (StringUtils.hasText(search)){
            sb.append(" AND (w_m.`name` LIKE @search or w_m.tel = @search or w_m.id_card_no = @search OR w_type.`name` = @search) ");

        }
        Sql sql = Sqls.create(sb.toString());
        sql.setParam("pId", pId);
        sql.setParam("search", "%" + search + "%");

        return this.listPage(pageNumber, pageSize, sql);
    }

    @Override
    public List<Record> getWorkerManAttendanceInfo(Long pId, Long id, String date) {
        boolean b = this.tableIsExist("bi_attendance_record_", StringUtil.last2num(pId.toString()));
        if (!b) {
            return new ArrayList<>();
        }
        String projectId = pId.toString();
        String afterProjectId = projectId.substring(projectId.length()-2, projectId.length());
        StringBuilder sb = new StringBuilder("SELECT DATE_FORMAT(record_dt, '%Y-%m-%d') record_dt, DATE_FORMAT(record_dt, '%w') `week`, working_hours, if(DATE_FORMAT(record_dt, '%Y-%m-%d') = DATE_FORMAT(NOW(), '%Y-%m-%d'),null, is_statistics_completed) is_statistics_completed, statistics_interval ");
        sb.append(" FROM bi_attendance_record_");
        sb.append(afterProjectId);
        sb.append(" WHERE project_id = @pId AND worker_id = @id ");
        sb.append(" AND record_dt > @date AND record_dt < DATE_ADD(@date,INTERVAL 1 MONTH) ");
        Sql sql = Sqls.create(sb.toString());
        sql.setParam("pId", pId);
        sql.setParam("id", id);
        sql.setParam("date", date);
        return this.list(sql);
    }

    @Override
    public List<Record> getTheDayAttendance(Long pId, Long id, String date) {
        boolean b = this.tableIsExist("bi_access_record_", StringUtil.last2num(pId.toString()));
        if (!b) {
            return new ArrayList<>();
        }
        String projectId = pId.toString();
        String afterProjectId = projectId.substring(projectId.length()-2, projectId.length());
        StringBuilder sb = new StringBuilder(" SELECT DATE_FORMAT(record_dtime,'%H:%i:%s') record_dtime, type ");
        sb.append(" FROM bi_access_record_");
        sb.append(afterProjectId);
        sb.append(" WHERE project_id = @pId AND workerman_id = @id ");
        sb.append(" AND record_dtime >= '$date 00:00:00' AND record_dtime <= '$date 23:59:59' ");
        sb.append(" ORDER BY record_dtime ASC ");
        Sql sql = Sqls.create(sb.toString());
        sql.setVar("date",date);
        sql.params().set("pId",pId).set("id", id);
        return this.list(sql);
    }

    @Override
    public Map<String, Object> getWorkerMan(Long id) {
        StringBuilder sb = new StringBuilder(" SELECT CONCAT(w_m.id, '') worker_man_id, CONCAT(w_m.head_photo_id,'') head_photo_id , w_m.`name` worker_man_name, w_t.`name` worker_team_name, w_m.sex, w_m.birth_date, TIMESTAMPDIFF(YEAR, w_m.birth_date, CURDATE()) age, w_m.tel, CONCAT(w_class.`name`,'-',w_type.`name`) worker_type, workman_in_date");
        sb.append(" from bi_workerman w_m ");
        sb.append(" LEFT JOIN bi_workerteam w_t on w_m.worker_team_id = w_t.id ");
        sb.append(" LEFT JOIN bi_worker_man_work_extend w_m_w_e on w_m_w_e.worker_man_id = w_m.id  ");
        sb.append(" LEFT JOIN sys_work_class w_class on w_m_w_e.work_class_id = w_class.id   ");
        sb.append(" LEFT JOIN sys_work_type w_type on w_m_w_e.work_type_id = w_type.id  ");
        sb.append(" LEFT JOIN bi_re_workman_and_teamproject r_w_a_t ON r_w_a_t.worker_man_id = w_m.id ");
        sb.append(" where w_m.id = @id ");
        Sql sql = Sqls.create(sb.toString());
        sql.setParam("id", id);
        List<Record> result = this.list(sql);
        if (result.size() > 0){
            return result.get(0);
        }
        return null;
    }

}
